package pack1;

import java.io.IOException;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Gestisce il login degli utenti
 */
public class SearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
      
    
    public SearchServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	if(request.getSession().getAttribute("authenticated") == null || !(boolean)request.getSession().getAttribute("authenticated")) {
    		response.sendRedirect("index.jsp");
			return;
    	}
		
    	RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/auth/search.jsp");
		dispatcher.forward(request, response); 
    }

	/**
	 * Ottiene il Context utilizzato in seguito per ottenere i riferimenti ai RemoteSystems
	 * @return Context utilizzato per la lookUp
	 * @throws Exception Eccezione lanciata nel caso il metodo non riesca ad ottenere il Context
	 */
	static public Context getInitialContext() throws Exception{
		
		Hashtable<String,String> env = new Hashtable<String,String>();
		
		env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		env.put(Context.PROVIDER_URL, "localhost:1099");
	
		return new InitialContext(env);
		
	}
	
}
